package org.sjs.dreamfly.common.util

import android.content.Context
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.source.DefaultMediaSourceFactory
import com.google.android.exoplayer2.source.hls.HlsMediaSource
import com.google.android.exoplayer2.upstream.DataSource
import com.google.android.exoplayer2.upstream.DefaultDataSource
import org.sjs.dreamfly.widget.PlayerView

object PlayUtil {

    fun testExoPlayer(context: Context, playerView: PlayerView) {
        val url = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
        val mediaSource = DefaultMediaSourceFactory(context).createMediaSource(MediaItem.fromUri(url))
        val player = ExoPlayer.Builder(context)
            .build()
        playerView.player = player
        player.playWhenReady = true
        player.setMediaSource(mediaSource)
        player.prepare()
    }

    /**
     * example: https://ckzy2.com/20230702/i7rezpqi/index.m3u8
     */
    fun playM3u8(context: Context, playerView: PlayerView, url: String, player: ExoPlayer? = null ): ExoPlayer {
        LogUtil.d("url:$url")
        val dataSourceFactory: DataSource.Factory = DefaultDataSource.Factory(context)
        val mediaSource = HlsMediaSource.Factory(dataSourceFactory).createMediaSource(MediaItem.fromUri(url))
        val realPlayer: ExoPlayer = player?:(ExoPlayer.Builder(context).build())
        playerView.player = player
        realPlayer.playWhenReady = true
        realPlayer.setMediaSource(mediaSource)
        realPlayer.prepare()
        return realPlayer
    }

    fun playM3u8(context: Context, player: ExoPlayer, url: String) {
        LogUtil.d("url:$url")
        val dataSourceFactory: DataSource.Factory = DefaultDataSource.Factory(context)
        val mediaSource = HlsMediaSource.Factory(dataSourceFactory).createMediaSource(MediaItem.fromUri(url))
        player.pause()
        player.setMediaSource(mediaSource)
        player.prepare()
    }

    fun playVideo(context: Context, playerView: PlayerView, url: String, player: ExoPlayer? = null ): ExoPlayer? {
        LogUtil.d("url:$url")
        if (url.isEmpty()) return null
        val realPlayer: ExoPlayer = player?:(ExoPlayer.Builder(context).build())
        val dataSourceFactory: DataSource.Factory = DefaultDataSource.Factory(context)
        val mediaSource = DefaultMediaSourceFactory(dataSourceFactory).createMediaSource(MediaItem.fromUri(url))
        playerView.player = realPlayer
        realPlayer.setMediaSource(mediaSource)
        realPlayer.prepare()
        return realPlayer
    }

    fun playVideo(context: Context, player: ExoPlayer, url: String) {
        LogUtil.d("url:$url")
        val dataSourceFactory: DataSource.Factory = DefaultDataSource.Factory(context)
        val mediaSource = DefaultMediaSourceFactory(dataSourceFactory).createMediaSource(MediaItem.fromUri(url))
        player.pause()
        player.setMediaSource(mediaSource)
        player.prepare()
        player.play()
    }
}